local HtmlUtil = require("common.HtmlUtil")
local Lang = require("common.Lang")
local ExcelShop = require("excel.Shop")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local ItemDefine = require("bag.ItemDefine")
local BagLogic = require("bag.BagLogic")
local Equip = require("bag.Equip")
local Grid = require("bag.Grid")
local ShopLogic = require("shop.ShopLogic")
local ActivityLogicL = require("activity.ActivityLogicL")
local MySteryShop = require("shop.MySteryShop")
local ExcelMapConf = require("excel.Map")
local Map = require("core.Map")
local RoleRevive = require("role.RoleRevive")
local MonthCardLogic = require("vip.MonthCardLogic")


local grid = {}


function get_grid(item_id, cnt, bind)
    grid.data = {}
    grid.data.equip = {}
    grid.data.gem = {}
    if ItemDefine.isEquip(item_id) then
        Equip.create(grid.data.equip, item_id, 0)
    end
    grid.id = item_id
    grid.cnt = cnt
    grid.lifeTime = 0
    grid.items = {}
    grid.bind = bind and bind ~= 0 and 1 or 0
    return grid
end

local function send_GC_SHOP_NPC_REBUY_QUERY(human)
    local mm = Msg.gc.GC_SHOP_NPC_REBUY_QUERY
    mm.grid[0] = 0
    for i=0, human.db.gridRebuyLen - 1 do
        mm.grid[0] = mm.grid[0] + 1
        Grid.makeItem(human.db.gridRebuy[i], mm.grid[mm.grid[0]], mm.grid[0], human)
    end
    Msg.send(mm, human.fd)
end

function CG_SHOP_NPC_BUY(human, m)
    local shop = ExcelShop.shops[m.shop_id]
    if not shop then
        return ShopLogic.send_err(human, Lang.SHOP_ERR_NO_SHOP)
    end
    local shop = ExcelShop[shop.name]
    if m.item_cnt < 1 then
        return ShopLogic.send_err(human, Lang.SHOP_ERR_ITEM_CNT_NOT_POSITIVE)
    end
    local good
    for _, v in ipairs(shop) do
        if v.id == m.item_id then
            good = v
            break
        end
    end
    if not good then
        return ShopLogic.send_err(human, Lang.SHOP_ERR_NO_SHOP_ITEM)
    end
    if not BagLogic.checkCanAddItem(human, good.id, 0, m.item_cnt) then
        return ShopLogic.send_err(human, Lang.SHOP_ERR_BAG_FULL)
    end
    local money_type = good.money_type
    if money_type == ItemDefine.ITEM_ID_YINLIANG then
        local nowYinliang = ObjHuman.getYinliang(human)
        if nowYinliang < good.price * m.item_cnt then
            return ShopLogic.send_err(human, Lang.SHOP_ERR_MONEY)
        end
        ObjHuman.decYinliang(human, good.price * m.item_cnt, "shop_buy_item", good.id, m.item_cnt)
    elseif money_type == ItemDefine.ITEM_ID_YUANBAO then
        if not ObjHuman.checkYuanbaoEnough(human, good.price * m.item_cnt) then
            return ShopLogic.send_err(human, Lang.SHOP_ERR_RMB)
        end
        ObjHuman.decYuanbao(human, good.price * m.item_cnt, "shop_buy_item", good.id, m.item_cnt)
    elseif ExcelShop.shops[m.shop_id].name == "exchange" or m.shop_id == 3 then
        local nItemCount = BagLogic.getItemCount(human, money_type)
        if nItemCount < good.price * m.item_cnt then
            return ShopLogic.send_err(human, string.format(Lang.SHOP_ERR_ITEM, ItemDefine.getValue(money_type, "name")))
        end
        BagLogic.delItemsById(human, money_type, good.price * m.item_cnt, "shop_buy_item")
    else
        assert()
    end
    BagLogic.addItem(human, good.id, 1, m.item_cnt, "shop_buy_item", 0, 0)
    local grid = get_grid(m.item_id, 1)
    ShopLogic.send_err(human, Lang.SHOP_BUY_OK, HtmlUtil.fontItemId(m.item_id, m.item_cnt, grid.data.equip and grid.data.equip.quality),
    money_type == ItemDefine.ITEM_ID_YINLIANG and HtmlUtil.fontYinliang(good.price * m.item_cnt) or
    money_type == ItemDefine.ITEM_ID_YUANBAO and HtmlUtil.fontYuanbao(good.price * m.item_cnt) or
    m.shop_id == 3 and HtmlUtil.fontItemId3(money_type, good.price * m.item_cnt, nil))
    Msg.send(Msg.gc.GC_SHOP_BUY_SUCCESS, human.fd)
end
function CG_SHOP_NPC_BUY_BATCH(human, m)
    local mm = Msg.cg[Msg.proto_id.CG_SHOP_NPC_BUY]
    mm.shop_npc_id = m.shop_npc_id
    mm.item_id = m.item_id
    mm.item_cnt = ItemDefine.canAppend(m.item_id) and 100 or 1
    CG_SHOP_NPC_BUY(human, mm)
end
function CG_SHOP_NPC_QUERY(human, m)
    CG_SHOP_NPC_QUERY_sub(human, m.shop_id)
end
function CG_SHOP_NPC_QUERY_sub(human, shop_id)
    local shop_npc = ExcelShop[ExcelShop.shops[shop_id].name]
    if not shop_npc then
        return ShopLogic.send_err(human, Lang.SHOP_ERR_NO_SHOP)
    end
    local mm = Msg.gc.GC_SHOP_NPC_QUERY
    mm.shop_id = shop_id
    mm.goods[0] = 0
    for _, v in ipairs(shop_npc) do
        mm.goods[0] = mm.goods[0] + 1
        local left = mm.goods[mm.goods[0]]
        left.oldPrice = 0
        left.price = v.price
        local grid = get_grid(v.id, 1, 4 < v.money_type)
        left.type = v.showType or 0
        left.shop_id = shop_id
        left.limitCnt = 0
        left.limitTime = 0
        left.totalCnt = 0
        left.money_type_item_icon = 0 
        left.money_type_item_name = "" 
        left.showPos = 0
        local cf = ItemDefine.getCf(v.money_type)
        if cf then
            left.money_type_item_icon =cf.icon 
            left.money_type_item_name = cf.name 
        end


        left.money_type = v.money_type
        Grid.makeItem(grid, left.grid, mm.goods[0], human)
    end
    Msg.send(mm, human.fd)
end

function CG_SHOP_NPC_REBUY(human, m)
    m.pos = math.max(m.pos - 1, 0)
    local grid = human.db.gridRebuy and human.db.gridRebuy[m.pos]
    if not grid then
        return ShopLogic.send_err(human, Lang.SHOP_REBUY_ERR_ITEM_NOT_EXIST)
    end
    local index = BagLogic.getEmptyIndex(human)
    if not index then
        return ShopLogic.send_err(human, Lang.SHOP_REBUY_ERR_BAG_FULL)
    end
    if not ItemDefine.getPrice(grid) then
        return
    end

    local price = ItemDefine.getPrice(grid) * grid.cnt
    local nowYinliang = ObjHuman.getYinliang(human)
    if nowYinliang < price then
        return ShopLogic.send_err(human, Lang.SHOP_ERR_MONEY)
    end

    ObjHuman.decYinliang(human, price, "npc_rebuy")
    BagLogic.addItemByGrid(human, index, grid, "npc_rebuy")
    for i = m.pos, human.db.gridRebuyLen - 1 do
        human.db.gridRebuy[i] = human.db.gridRebuy[i + 1]
    end
    human.db.gridRebuyLen = human.db.gridRebuyLen - 1
    send_GC_SHOP_NPC_REBUY_QUERY(human)
    ShopLogic.send_err(human, Lang.SHOP_REBUY_OK)
end
function CG_SHOP_NPC_REBUY_QUERY(human)
    send_GC_SHOP_NPC_REBUY_QUERY(human)
end
--出售
local function CG_SHOP_NPC_SELL_sub(human, pos_bag, no_send)
    if pos_bag <=-1 or pos_bag >= ItemDefine.BAG_MAX_CAP then
        return ShopLogic.send_err(human, "背包格子错误")
    end
    local grid = human.db.bag[pos_bag]
    if not grid then
        return ShopLogic.send_err(human, Lang.SHOP_SELL_ERR_GRID)
    end
    local subType1 = ItemDefine.getValue(grid.id, "subType1")
    local subType2 = ItemDefine.getValue(grid.id, "subType2")
    local canSell = ItemDefine.getValue(grid.id, "canSell")
    if (subType1 == 12 and subType2 == 10) or canSell == 1 then
        return ShopLogic.send_err(human, Lang.BAG_ITEM_NOT_SALE)
    end
    if not ItemDefine.getPrice(grid) or not grid.cnt then
        return
    end
    local price = ItemDefine.getPrice(grid) * grid.cnt
    if not ObjHuman.canUpdateYinliang(human, price) then
        return ShopLogic.send_err(human, Lang.MARKET_ERR_TONGBI_FULL)
    end

    local cap = #Msg.gc.GC_SHOP_NPC_REBUY_QUERY.grid
    if cap <= human.db.gridRebuyLen then
        human.db.gridRebuyLen = cap - 1
    end
    if human.db.gridRebuyLen > 0 then
        for i = human.db.gridRebuyLen - 1, 0, -1 do
            human.db.gridRebuy[i+1] = human.db.gridRebuy[i]
        end
    end
    Grid.copy(human.db.gridRebuy[0], grid)
    human.db.gridRebuyLen = human.db.gridRebuyLen + 1
    ObjHuman.addYinliang(human, price, "npc_sell", grid.id, grid.cnt)
    BagLogic.delGridByIndex(human, pos_bag, "npc_sell")
    if no_send then
        return price
    else
        send_GC_SHOP_NPC_REBUY_QUERY(human)
        local str = string.format(Lang.SHOP_SELL_OK, price)
        ShopLogic.send_err(human, str)
    end
end
--出售
function CG_SHOP_NPC_SELL(human, m)
    CG_SHOP_NPC_SELL_sub(human, m.pos_bag)
end

function CG_MALL_QUERY(human)
    ShopLogic.queryMall(human)
end
function CG_MALL_BUY(human, msg)
    local ret = ShopLogic.quickBuy(human, msg.item_id, msg.item_cnt, msg.mall_id, true)
    if ret then
        if human.db.hp < 1 then
            RoleRevive.reviveNow(human)
        end
        ActivityLogicL.updateActivityValue(human, "shop")
    end
end
function CG_MALL_BATCH_BUY(human, msg)
    local itemList = {}
    for i=1, msg.item_id[0] do
        itemList[i] = {}
        itemList[i][1] = msg.item_id[i]
        itemList[i][2] = msg.item_cnt[i]
    end
    ShopLogic.batchBuy(human, itemList)
end

function CG_LIMIT_BUY_SHOW_INFO(human)
    ShopLogic.sendLimitShowInfo(human)
end
function CG_MALL_LIMIT_BUY(human, msg)
    local ret = ShopLogic.LimitBuy(human, msg.itemId, msg.cnt)
    if ret then
        ActivityLogicL.updateActivityValue(human, "shop")
    end
end

function CG_MYSTERY_SHOP_QUERY(human, msg)
    MySteryShop.CG_MYSTERY_SHOP_QUERY(human, msg)
end

function CG_MYSTERY_SHOP_FLUSH(human, msg)
    MySteryShop.CG_MYSTERY_SHOP_FLUSH(human, msg)
end

function CG_MYSTERY_SHOP_BUY(human, msg)
    MySteryShop.CG_MYSTERY_SHOP_BUY(human, msg)
end


function CG_MYSTERY_SHOP_FLUSHDATA(human, msg)
    MySteryShop.CG_MYSTERY_SHOP_FLUSHDATA(human, msg)
end
